home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
ici
/
ici.cpi
/
icimain.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-27
|
6KB
|
281 lines
#include "ptr.h"
#include "exec.h"
#include "file.h"
#include "str.h"
#include "struct.h"
#include "buf.h"
#include "wrap.h"
#include "func.h"
int
ici_main(argc, argv)
int argc;
char *argv[];
{
register int i;
register int j;
char *s;
char *fmt;
char *arg0;
array_t *av;
FILE *stream;
file_t *f;
if
(
ici_init()
||
mkvar(structof(v_top[-1])->s_super, "stdin", 'u', stdin)
||
mkvar(structof(v_top[-1])->s_super, "stdout", 'u', stdout)
||
mkvar(structof(v_top[-1])->s_super, "stderr", 'u', stderr)
)
goto fail;
/*
* Process arguments. Two pass, first gather "unused" arguments,
* ie, arguments which are passed into the ICI code. Stash these in
* the array av. NB: must be in sync with the second pass below.
*/
if ((av = new_array()) == NULL)
goto fail;
if (pushcheck(av, 1))
goto fail;
*av->a_top++ = objof(&o_null); /* Leave room for argv[0]. */
arg0 = NULL;
if (argc > 1 && argv[1][0] != '-')
{
/*
* Usage1: ici file [args...]
*/
arg0 = argv[1];
for (i = 2; i < argc; ++i)
{
if (pushcheck(av, 1))
goto fail;
if ((*av->a_top = objof(get_cname(argv[i]))) == NULL)
goto fail;
++av->a_top;
}
}
else
{
/*
* Usage2: ici [-f file] [-p prog] etc... [--] [args...]
*/
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '-')
{
for (j = 1; argv[i][j] != '\0'; ++j)
{
switch (argv[i][j])
{
case 'm':
if (argv[i][++j] != '\0')
s = &argv[i][j];
else if (++i >= argc)
goto usage;
else
s = argv[i];
if ((av->a_base[0] = objof(get_cname(s))) == NULL)
goto fail;
break;
case '-':
while (++i < argc)
{
if (pushcheck(av, 1))
goto fail;
if ((*av->a_top = objof(get_cname(argv[i])))==NULL)
goto fail;
++av->a_top;
}
break;
case 'f':
case 'l':
case 'e':
if (argv[i][++j] != '\0')
arg0 = &argv[i][j];
else if (++i >= argc)
goto usage;
else
arg0 = argv[i];
break;
case 'w':
if (argv[i][++j] != '\0')
s = &argv[i][j];
else if (++i >= argc)
goto usage;
else
s = argv[i];
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
continue;
default:
goto usage;
}
break;
}
}
else
{
if (pushcheck(av, 1))
goto fail;
if ((*av->a_top = objof(get_cname(argv[i]))) == NULL)
goto fail;
++av->a_top;
}
}
}
if (av->a_base[0] == objof(&o_null))
{
if (arg0 == NULL)
arg0 = argv[0];
if ((av->a_base[0] = objof(get_cname(arg0))) == NULL)
goto fail;
}
else
arg0 = stringof(av->a_base[0])->s_chars;
if (av->a_top - av->a_base == 2 && stringof(av->a_base[1])->s_nchars == 0)
--av->a_top; /* Patch around Bourne shell "$@" with no args bug. */
if
(
mkvar(structof(v_top[-1])->s_super, "argv", 'o', objof(av))
||
mkvar(structof(v_top[-1])->s_super, "argc", 'i', (long)(av->a_top - av->a_base))
)
goto fail;
/*
* Pass two over the arguments; actually parse the modules.
*/
if (argc > 1 && argv[1][0] != '-')
{
if ((stream = fopen(argv[1], "r")) == NULL)
{
sprintf(buf, "%s: Could not open %s.", argv[0], argv[1]);
error = buf;
goto fail;
}
if (parse_file(argv[1], (char *)stream, &stdio_ftype))
goto fail;
}
else
{
for (i = 1; i < argc; ++i)
{
if (argv[i][0] != '-')
continue;
if (argv[i][1] == '\0')
{
if (parse_file("stdin", (char *)stdin, &stdio_ftype))
goto fail;
continue;
}
for (j = 1; argv[i][j] != '\0'; ++j)
{
switch (argv[i][j])
{
case '-':
i = argc;
break;
case 'e':
if (argv[i][++j] != '\0')
s = &argv[i][j];
else if (++i >= argc)
goto usage;
else
s = argv[i];
if ((f = sopen(s, strlen(s))) == NULL)
goto fail;
if ((f->f_name = get_cname("")) == NULL)
goto fail;
if (parse_module(f, structof(v_top[-1])) < 0)
goto fail;
loose(f);
break;
case 'l':
#ifdef MSDOS
fmt = "C:\\ICI\\LIB%s.ICI";
#else
fmt = "/usr/local/include/ici/%s.ici";
#endif
goto dofile;
case 'f':
fmt = "%s";
dofile:
if (argv[i][++j] != '\0')
s = &argv[i][j];
else if (++i >= argc)
goto usage;
else
s = argv[i];
if (chkbuf(strlen(s) + strlen(fmt)))
goto fail;
sprintf(buf, fmt, s);
if ((stream = fopen(buf, "r")) == NULL)
{
sprintf(buf, "%s: Could not open %s.", argv[0], s);
error = buf;
goto fail;
}
if (parse_file(buf, (char *)stream, &stdio_ftype))
goto fail;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if ((stream = fdopen(argv[i][j] - '0', "r")) == NULL)
{
sprintf(buf, "%s: Could not access file descriptor %d.",
argv[0], argv[i][j] - '0');
error = buf;
goto fail;
}
if (parse_file(arg0, (char *)stream, &stdio_ftype))
goto fail;
continue;
}
break;
}
}
}
/*
* For tracking down object leaks...
got(fetch(v_top[-1], get_cname("stdout")));
v_top = vs->a_base;
o_top = os->a_base;
x_top = xs->a_base;
ICI_reclaim();
ICI_reclaim();
pause();
*/
wrapup();
return 0;
usage:
fprintf(stderr, "usage:\t%s [-f file] [-e prog] [-digit] [-m name] [--] args...\n", argv[0]);
fprintf(stderr, "\t%s file args...\n", argv[0]);
wrapup();
error = "invalid command line arguments";
return 1;
fail:
s = error;
wrapup();
fflush(stdout);
fprintf(stderr, "%s\n", s);
return 1;
}